package com.springmvc.base;

import com.springmvc.annotation.RequestMapping;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HandlerMapping {
    private Map<String ,Handler> handlerMap = new HashMap<>();

    public void process(List<Object> beans) {
        for (Object bean : beans) {
            Method[] methods = bean.getClass().getDeclaredMethods();
            for (Method method : methods) {
                if (method.isAnnotationPresent(RequestMapping.class)) {
                    RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
                    String url = requestMapping.value();
                    handlerMap.put(url,new Handler(bean,method));
                }
            }
        }
    }

    public Handler getHandler(String url){
        return handlerMap.get(url);
    }
}
